/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.buzzwords.model;

import com.buzzwords.clientside.model.ClientLocale;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * Locale represents different areas in the world. Buzzword meanings may
 * differ between different locales. Locale has many categories
 *
 * @author potlow
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Locale {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    // Sub admin area buzzword submitted for
    @Persistent
    private String city = "";

    // Admin area buzzword submitted for
    @Persistent
    private String state = "";

    // Country buzzword submitted for
    @Persistent
    private String country = "";

    // different defintions of buzzword in locale
    @Persistent(mappedBy="locale")
    private List<Definition> definitions = new ArrayList<Definition>();

    // Owning buzzword
    @Persistent
    private Buzzword buzzword;

    // Creation date of locale
    @Persistent
    private Date creationDate;

    /**
     * Constructor takes area the locale represents and buzzword that locale
     * represents
     * @param area Formatted string that represents are
     * @param buzzword Buzzword locale belongs to
     */
    public Locale(String area, Buzzword buzzword) {
        this.buzzword = buzzword;
        this.creationDate = new Date();

        Map<String,String> cityStateCountry = extractCityStateCountryFromArea(area);
        this.city = cityStateCountry.get("city");
        this.state = cityStateCountry.get("state");
        this.country = cityStateCountry.get("country");
    }

    /**
     * Default constructor
     */
    public Locale() {
    }

    public Buzzword getBuzzword() {
        return buzzword;
    }

    public void setBuzzword(Buzzword buzzword) {
        this.buzzword = buzzword;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public List<Definition> getDefinitions() {
        return definitions;
    }

    public void setDefinitions(List<Definition> definitions) {
        this.definitions = definitions;
    }

    public ClientLocale getClientLocale() {
        ClientLocale clientLocale = new ClientLocale();
        clientLocale.setKey(KeyFactory.keyToString(key));
        clientLocale.setCity(city);
        clientLocale.setCountry(country);
        clientLocale.setState(state);

        for(Definition definition : definitions) 
            clientLocale.getDefinitions().add(definition.getClientDefinition());

        return clientLocale;
    }

    /**
     * Extracts city, state, and country from a formatted string
     * <city>,<state>,<country>
     * @param area formatted string
     * @return Map of attributes
     */
    public static Map<String,String> extractCityStateCountryFromArea(String area) {
        Hashtable<String,String> hashtable = new Hashtable<String,String>();
        
        // get country
        int beginPosition = area.lastIndexOf(',');
        hashtable.put("country", area.substring(beginPosition+1));
        String cityState = area.substring(0, beginPosition);

        // get city and state
        beginPosition = cityState.lastIndexOf(',');
        hashtable.put("state", cityState.substring(beginPosition+1));
        hashtable.put("city", cityState.substring(0, beginPosition));

        return hashtable;
    }

    private String combineCityStateCountryToArea() {
        String area = this.city+ "," +this.state+ "," +this.country;
        return area;
    }

    public boolean isRepresentedBy(String area) {
        if(area.equalsIgnoreCase(combineCityStateCountryToArea()))
            return true;

        return false;
    }

}
